<template>
	<view class="approval-list">
		<view class="ctn_header"><liuyuno-tabs :isShowTap="false" @confirm="confirmDate" @rest="restSearch"></liuyuno-tabs></view>
		<view class="ctn_middlee">
			<view v-for="(item, index) in listData" :key="index" v-if="listData.length != 0">
				<my-Approval
					billtype='HA02'
					:status="item.appStatus"
					:isShowTakeBtn="item.appStatus == 3"
					:propTitlel="item.propTitlel"
					:propdays="item.realleavedays"
					:pk="item.pk"
					:timeArr="item.timeArr"
					:showBtn="item.appStatus == -1"
					@confirm="confirmAdvice"
					@prograss="checkPrograss(item.pk)"
					@takeback="takeback(item.pk)"
					@deleteRec="deleteRec(item.pk)"
					@edit="edit(item)"
				></my-Approval>
			</view>
		</view>
		<view class="jumpHome" @click="jumpHome">首页</view>
	</view>
</template>

<script>
import utils from '../../../common/utils/utils.js';
import service from '../../../common/service/new_file.js';
import apiData from '../../../common/utils/apiData.js';
export default {
	data() {
		return {
			tabs: ['待审核', '已审核'],
			defaultIndex: 0,
			form: {
				startTime: '',
				endTime: ''
			},
			leaveTypeOptions: [],
			listData: [],
			fileList: ['1']
		};
	},
	methods: {
		jumpHome() {
			uni.navigateTo({
				url: '../../home/home'
			});
		},
		checkPrograss(pk) {
			uni.navigateTo({
				url: '../../attendance/attendacePrograss?pk=' + pk + '&billtype=HA02'
			});
		},
		edit(item) {
			uni.navigateTo({
				url: './destroyComit?item=' + JSON.stringify(item)
			});
		}, //修改内容

		//++++++++++++++++++++++附件的操作++++++++++++++
		//++++++++++++++++++++++附件的操作++++++++++++++
		confirmAdvice(val) {
			let _this = this;
			let billtype = 'HA02';
			let data = apiData.assign(val.pk, val.spr, billtype, 'sendapprove', 'Y');
			service.common(data, {
				success: res => {
					uni.showToast({
						icon: 'success',
						title: '操作成功!'
					});
					_this.getLeaveRecord(_this.form.startTime, _this.form.endTime);
				}
			});
		},
		deleteRec(pk) {
			let _this = this;
			uni.showModal({
				title: '提示',
				content: '你确定删除当前单据吗？',
				success: function(res) {
					if (res.confirm) {
						let data = apiData.delLeaveRecord(pk, 'HA02');
						service.common(data, {
							success: function(res) {
								uni.showToast({
									title: '删除成功'
								});
								setTimeout(() => {
									_this.getLeaveRecord(_this.form.startTime, _this.form.endTime);
								}, 500);
							},
							fail: function(res) {}
						});
					} else if (res.cancel) {
						uni.showToast({
							icon: 'none',
							title: '已取消操作'
						});
					}
				}
			});
		},
		takeback(pk) {
			let _this = this;
			let data = apiData.takebackRecord(pk, 'HA02');
			service.common(data, {
				success: function(res) {
					uni.showToast({
						title: '操作成功'
					});
					setTimeout(() => {
						_this.getLeaveRecord(_this.form.startTime, _this.form.endTime);
					}, 500);
				},
				fail: function(res) {}
			});
		},
		confirmDate(time) {
			let startTime = time[0] + ' ' + '00:00:00';
			let endTime = time[1] + ' ' + '23:59:59';
			this.getLeaveRecord(startTime, endTime);
		},
		restSearch() {
			this.getLeaveRecord(this.form.startTime, this.form.endTime);
		},
		getLeaveRecord(startTime, endTime) {
			let _this = this;
			let data = apiData.getLeaveRecord('HA02', startTime, endTime);
			service.common(data, {
				success: res => {
					function compare(property) {
						return function(a, b) {
							var value1 = new Date(a[property].replace(/-/g, '/')).getTime();
							var value2 = new Date(b[property].replace(/-/g, '/')).getTime();
							return value2 - value1; //升序,降序为value2 - value1
						};
					}
					function timeFormat(time) {
						let date = new Date(time.replace(/-/g, '/'));
						let yy = date.getFullYear();
						let mm = date.getMonth() + 1;
						let dd = date.getDate();
						return yy + '-' + mm + '-' + dd;
					}
					if (res.data.VALUES.length != 0) {
						// console.log(res)
						let newData = [];
						let resArr = res.data.VALUES;
						// console.log(resArr)
						resArr.forEach(item => {
							debugger
							let hvo = item.data.hvo;
							let sumhours = 0;
							let allTimeArr = [];
							let start;
							let endTime;
							if (hvo.leavebeginpro == 1) {
								start = timeFormat(hvo.realbegintime) + '全天';
							} else if (hvo.leavebeginpro == 2) {
								start = timeFormat(hvo.realbegintime) + '上午';
							} else if (hvo.leavebeginpro == 3) {
								start = timeFormat(hvo.realbegintime) + '下午';
							}

							if (hvo.leaveendpro == 1) {
								endTime = timeFormat(hvo.realendtime) + '全天';
							} else if (hvo.leaveendpro == 2) {
								endTime = timeFormat(hvo.realendtime) + '上午';
							} else if (hvo.leaveendpro == 3) {
								endTime = timeFormat(hvo.realendtime) + '下午';
							}
							allTimeArr.push({
								start: hvo.realbegintime,
								viewStarttime: start, //申请页渲染的数据
								endTime: hvo.realendtime,
								viewEndtime: endTime,
								tsbody: hvo.ts,
								pk_leave_b: hvo.pk_leavetype.pk,
								leavehours: hvo.realleavedays
							});
							newData.push({
								appStatus: hvo.approvestatus,
								propTitler: sumhours,
								propTitlel: hvo.pk_leavetype.name,
								timeArr: allTimeArr,
								pk_leavetype: hvo.pk_leavetype.pk,
								pk: hvo.pk_leaveoff,
								tshead: hvo.ts,
								billno: hvo.billno,
								leaveshift: hvo.leaveoffshift,
								billmaker: hvo.billmaker.pk,
								pk_period: hvo.pk_period.pk,
								//销假基础信息
								realleavedays: hvo.realleavedays,
								leavebeginpro:hvo.leavebeginpro,
								leaveendpro:hvo.leaveendpro,
								leavedays: hvo.leavedays,
								leaveshiftpro:hvo.leaveshiftpro,
								restedday: hvo.restedday,
								ts:hvo.ts,
								pk_leaveoff:hvo.pk_leaveoff
							});
						});
						// newData.sort(compare('ts'))//排序
						_this.listData = newData;
					} else {
						this.listData = [];
						uni.showToast({
							icon: 'none',
							title: '当前列表为空'
						});
					}
				}
			});
		},
		tabClick(e) {}
	},
	onReady() {
		this.getLeaveRecord(this.form.startTime, this.form.endTime);
	},
	onPullDownRefresh() {
		this.getLeaveRecord(this.form.startTime, this.form.endTime);
	},
	onLoad() {}
};
</script>

<style lang="scss" scoped>
.jumpHome {
	width: 80rpx;
	position: fixed;
	bottom: 350rpx;
	right: 20rpx;
	height: 40rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 16rpx;
	background: #a7a7a7;
	color: #f1f1f1;
	font-size: 24rpx;
}
.approval-list {
	height: 100vh;
	display: flex;
	background: #f7f7f7;
	flex-direction: column;
	.ctn_header {
	}

	.ctn_middlee {
		flex: 1;
		padding: 0 36rpx;
		margin-top: 140rpx;
	}
}
</style>
